package com.yuyou.fn.educational.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.vo.ClassReportConfig;
import com.yuyou.fn.educational.vo.ReportConfigDetailVo;
import com.yuyou.fn.educational.vo.ReportConfigSaveOrUpdateVo;
import org.springframework.stereotype.Service;

import com.yuyou.fn.educational.mapper.ReportConfigMapper;
import com.yuyou.fn.educational.entity.ReportConfig;
import com.yuyou.fn.educational.service.IReportConfigService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * ReportConfig 表数据服务层接口实现类
 *
 */
@Service
public class ReportConfigServiceImpl extends SuperServiceImpl<ReportConfigMapper, ReportConfig,Long> implements IReportConfigService {

    @Resource
    private ICourseClassService courseClassService;

    @Override
    public List<ReportConfig> findByPeriodIdsResourceId(List<Long> periodIds, long resourceId) {
        Example<ReportConfig> example=ReportConfig.createExample();
        example.createCriteria().andIn("periodId",periodIds).andEqualTo("resourceId",resourceId);
        return this.findListByExample(example);
    }

    @Override
    @Transactional
    public void saveReportConfig(ReportConfigSaveOrUpdateVo reportConfigSaveOrUpdateVo) {
        Long resourceId=reportConfigSaveOrUpdateVo.getResourceId();
        List<ReportConfigSaveOrUpdateVo.PeriodConfig> periodConfigs = reportConfigSaveOrUpdateVo.getPeriodConfigs();
        List<Long> periodIds= BeanUtils.toList(periodConfigs,"periodId");
        List<ReportConfig> checkList = this.findReportConfigByPeriodIdsResourceId(periodIds, resourceId);
        Map<Long, ReportConfig> periodConfigMap = BeanUtils.toMap(checkList, "periodId");

        List<ReportConfig> reportConfigList=new ArrayList<>();
        for (ReportConfigSaveOrUpdateVo.PeriodConfig periodConfig:periodConfigs){
            ReportConfig reportConfig = new ReportConfig();
            reportConfig.setConfig(JSONObject.toJSONString(periodConfig.getConfig()));
            reportConfig.setUpdateTime(TimeUtils.now());
            reportConfig.setOperatorId(Principals.get().id);
            reportConfig.setPeriodId(periodConfig.getPeriodId());
            reportConfig.setResourceId(resourceId);
            reportConfig.setType(periodConfig.getType());

            if (!periodConfigMap.containsKey(periodConfig.getPeriodId())){
                reportConfig.setCreateTime(TimeUtils.now());
                reportConfigList.add(reportConfig);
            }else {
                reportConfig.setReportConfigId(periodConfigMap.get(periodConfig.getPeriodId()).getReportConfigId());
                this.updateSelectiveById(reportConfig);
            }
        }
        if (!reportConfigList.isEmpty()){
            this.insertBatch(reportConfigList);
        }


    }

    @Override
    public List<ReportConfig> findReportConfigByPeriodIdsResourceId(List<Long> periodIds, long resourceId) {
        if (Utils.checkListEmpty(periodIds)){
            return Collections.EMPTY_LIST;
        }
        Example<ReportConfig> example=ReportConfig.createExample();
        example.createCriteria().andEqualTo("resourceId",resourceId).andIn("periodId",periodIds);
        return this.findListByExample(example);
    }

    @Override
    public List<ReportConfig> findReportConfigByPeriodIdsResourceIdType(List<Long> periodIds, long resourceId, int type) {
        if (Utils.checkListEmpty(periodIds)){
            return Collections.EMPTY_LIST;
        }
        Example<ReportConfig> example=ReportConfig.createExample();
        example.createCriteria().andEqualTo("resourceId",resourceId).andIn("periodId",periodIds).andEqualTo("type",type);
        return this.findListByExample(example);
    }

    @Override
    public List<ClassReportConfig> findReportClassByPeriodIdsResourceId(List<Long> periodIds, long resourceId,int type) {
        List<ClassReportConfig> classReportConfigs=new ArrayList<>(periodIds.size());
        List<ReportConfig> reportConfigList=this.findReportConfigByPeriodIdsResourceId(periodIds,resourceId);
        for (ReportConfig reportConfig:reportConfigList){
            ReportConfigDetailVo reportConfigDetailVo=JSONObject.parseObject(reportConfig.getConfig(),ReportConfigDetailVo.class);
            List<Long> classIds=reportConfigDetailVo.getExcludeClassIds();
            List<CourseClass> courseClassList = courseClassService.findByIds(classIds, CourseClass.BASE_FIELDS);
            ClassReportConfig classReportConfig=new ClassReportConfig();
            classReportConfig.setCourseClassList(courseClassList);
            classReportConfig.setReportConfig(reportConfig);
            classReportConfigs.add(classReportConfig);
        }
        return classReportConfigs;
    }
}